package com.leeafei.study.springboot.controller;

import com.alibaba.fastjson.JSONObject;
import com.leeafei.study.springboot.dto.Goods;
import com.leeafei.study.springboot.dto.GoodsMap;
import com.leeafei.study.springboot.dto.Spec;
import com.leeafei.study.springboot.dto.GoodsList;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class HelloController {

    /**
     * 基本类型数据绑定，可接收url param和RequestBody（form-data、x-www-form-urlencoded）数据，参数为必填
     * url: http://localhost/basicType?number=5
     */
    @RequestMapping("basicType1")
    public String basicType(int number) {
        return "number:" + number;
    }

    /**
     * 基本类型数据绑定，可接收url param和RequestBody（form-data、x-www-form-urlencoded）数据，参数为必填
     * url: http://localhost/basicType2?discount=true
     * url: http://localhost/basicType2?discount=1
     */
    @RequestMapping("basicType2")
    public String basicType(boolean discount) {
        return "discount:" + discount;
    }


    /**
     * 包装类型数据绑定，可接收url param和RequestBody（form-data、x-www-form-urlencoded）数据，参数为非必填
     * url: http://localhost/packType?num=4
     */
    @RequestMapping("packType")
    private String packType(@RequestParam(value = "num", required = false) Integer number) {
        return "number: " + number;
    }

    /**
     * 对象绑定，可接收url param和RequestBody（form-data、x-www-form-urlencoded）数据
     * url: http://localhost/spec1?color=白色&size=170
     */
    @RequestMapping("spec1")
    public String addSpec1(Spec spec) {
        return spec.toString();
    }

    /**
     * 对象绑定，只能接收RequestBody（raw）数据
     * json: {"color":"蓝色","size":175}
     */
    @RequestMapping("spec2")
    public String addSpec2(@RequestBody Spec spec) {
        return spec.toString();
    }

    /**
     * 多层级对象绑定，可接收url param和RequestBody（form-data、x-www-form-urlencoded）数据
     * url: http://localhost/goods1?name=上衣&price=98.9&stock=998&discount=false&spec.color=白色&spec.size=175
     */
    @RequestMapping("goods1")
    public String addGoods1(Goods goods) {
        return goods.toString();
    }

    /**
     * 多层级对象绑定，只能接收RequestBody（raw）数据
     * json: {"name":"牛仔裤","price":158,"stock":200,"discount":true,"spec":{"color":"蓝色","size":31}}
     */
    @RequestMapping("goods2")
    public String addGoods2(@RequestBody Goods goods) {
        return goods.toString();
    }


    /**
     * 用String接收RequestBody（raw）数据
     * json: {"name":"牛仔裤","price":158,"stock":200,"discount":true,"spec":{"color":"蓝色","size":31}}
     */
    @RequestMapping("goods3")
    public String addGoods3(@RequestBody String goodsJson) {
        return goodsJson;
    }

    /**
     * 数组绑定
     * url: http://localhost/array?name=aaa&name=bbb
     */
    @RequestMapping("array")
    public String array1(String[] name) {
        String nameStr = "";
        for (String n : name) {
            nameStr += n + "\n";
        }
        return nameStr;
    }

    /**
     * Map绑定，可接收url param和RequestBody（form-data、x-www-form-urlencoded）数据
     * http://localhost/map1?map['nike'].name=耐克&map['nike'].price=899&map['adidas'].name=阿迪达斯&map['adidas'].price=998
     */
    @RequestMapping("map1")
    public String map1(GoodsMap goodsMap) {
        String mapStr = "";
        Map<String, Goods> map = goodsMap.getMap();
        for (Map.Entry<String, Goods> entry : map.entrySet()) {
            mapStr += entry.getKey() + ":" + entry.getValue() + "\n";
            ;
        }
        return mapStr;
    }

    /**
     * Map绑定，只能接收RequestBody（raw）数据
     * url: http://localhost/map2
     * json: {"map":{"adidas":{"name":"衣服","price":98},"nike":{"name":"裤子","price":89}}}
     */
    @RequestMapping("map2")
    public String map2(@RequestBody GoodsMap goodsMap) {
        String mapStr = "";
        Map<String, Goods> map = goodsMap.getMap();
        for (Map.Entry<String, Goods> entry : map.entrySet()) {
            mapStr += entry.getKey() + ":" + entry.getValue() + "\n";
            ;
        }
        return mapStr;
    }

    /**
     * list绑定，可接收url param和RequestBody（form-data、x-www-form-urlencoded）数据
     * url: http://localhost/list1
     */
    @RequestMapping("list1")
    public String list1(GoodsList goodsList) {
        String listStr = "";
        for (Goods goods : goodsList.getList()) {
            listStr += goods.toString() + "\n";
        }
        System.out.println(JSONObject.toJSON(goodsList));
        return listStr;
    }

    /**
     * list绑定，只能接收RequestBody（raw）数据
     * url: http://localhost/list2
     * json: {"list":[{"name":"衣服","price":100},{"name":"裤子","price":98}]}
     */
    @RequestMapping("list2")
    public String list2(@RequestBody GoodsList goodsList) {
        String listStr = "";
        for (Goods goods : goodsList.getList()) {
            listStr += goods.toString() + "\n";
        }
        return listStr;
    }

}